package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotOnlineLogService;
import com.lechi.web.system.domain.IotOnlineLog;
import com.lechi.web.system.mapper.iot.IotOnlineLogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 在线日志记录Service业务层处理
 *
 * @author yangguang
 * @date 2024 -07-10
 */
@Service("IIotOnlineLogService")
@RequiredArgsConstructor
public class IotOnlineLogServiceImpl implements IIotOnlineLogService {

    private final IotOnlineLogMapper iotOnlineLogMapper;

    /**
     * 查询在线日志记录列表
     *
     * @param iotOnlineLog 在线日志记录
     * @return 在线日志记录
     */
    @Override
    public List<IotOnlineLog> selectIotOnlineLogList(IotOnlineLog iotOnlineLog) {
        try {
            return iotOnlineLogMapper.selectOnlineLogList(iotOnlineLog);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询在线日志记录列表", method, iotOnlineLog));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增在线日志记录
     *
     * @param iotOnlineLog 在线日志记录
     */
    @Override
    public void insertIotOnlineLog(IotOnlineLog iotOnlineLog) {
        try {
            iotOnlineLogMapper.insertIotOnlineLog(iotOnlineLog);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增在线日志记录", method, iotOnlineLog));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

}
